class Language
  include Mongoid::Document
  include Mongoid::Timestamps
  field :name, :type => String
  field :number, :type => Integer, :default => 0
  validates  :name, :presence => true, :uniqueness => { :case_sensitive =>false }
  index({ :name => 1}, { :unique => true,:background=>true })
  default_scope ->{ desc(:number) }

  before_save :refactor_number

  protected

  def refactor_number
    if self.number < 0
      self.number = 0
    end
  end
end
